package com.mmlc.meimei.lichengkongjian.Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.responsebean.NewProductListResponseBean;
import com.mmlc.utils.ViewUtils;
import com.mmlc.utils.XUtils;

import java.util.List;

/**
 * Created by Administrator on 2016/7/14.
 */
public class NewProductListAdapter extends RecyclerView.Adapter {

    List<NewProductListResponseBean.DataBean.GoodslistBean> datas;
    Context context;
    private OnItemClickListener mOnItemClickListener;

    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
    //点击监听接口
    public interface OnItemClickListener{
        void onItemClick(View view, int position);
    }


    public NewProductListAdapter(List<NewProductListResponseBean.DataBean.GoodslistBean> datas, Context context) {
        this.datas = datas;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_goods_list, parent, false);
        return new GridHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder vh, int i) {
        if(vh instanceof GridHolder){
            final GridHolder holder = (GridHolder) vh;
            NewProductListResponseBean.DataBean.GoodslistBean product = datas.get(i);
            if (holder.iv_avatar != null && !ViewUtils.isEmpty(product.getImg_thumb())) {
                XUtils.image(holder.iv_avatar, product.getImg_thumb(), XUtils.getImageOptionsBuilder().setImageScaleType(ImageView.ScaleType.CENTER_INSIDE).build());
            }
            if (holder.tv_brand != null && !ViewUtils.isEmpty(product.getBrand_name())) {
                holder.tv_brand.setText(product.getBrand_name());
            }
            if (holder.tv_name != null && !ViewUtils.isEmpty(product.getGoods_name())) {
                holder.tv_name.setText(product.getGoods_name());
            }

            if (holder.tv_price != null && !TextUtils.isEmpty(product.getPrice())) {
                holder.tv_price.setText("¥ " + product.getPrice());
            }

            if(!TextUtils.isEmpty(product.getDiscount())) {
                holder.tv_market_price.setText(product.getDiscount());
            }else {
                holder.ll_discount.setVisibility(View.GONE);
            }
            if (TextUtils.equals("0",product.getIs_license()))
            {
                holder.iv_mark.setVisibility(View.VISIBLE);
            }else {
                holder.iv_mark.setVisibility(View.GONE);
            }
            //判断是否设置了监听器
            //单击判断
            if(mOnItemClickListener != null){
                //为ItemView设置监听器
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = holder.getLayoutPosition(); // 1
                        mOnItemClickListener.onItemClick(holder.itemView,position); // 2
                    }
                });
            }
        }

    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    class GridHolder extends RecyclerView.ViewHolder{
        ImageView iv_avatar,iv_mark;
        TextView tv_brand,tv_name, tv_price,tv_market_price;
        LinearLayout ll_discount;
        public GridHolder(View itemView) {
            super(itemView);
            iv_avatar = (ImageView) itemView.findViewById(R.id.iv_avatar);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_brand = (TextView) itemView.findViewById(R.id.tv_brand);
            tv_price = (TextView) itemView.findViewById(R.id.tv_price);
            iv_mark = (ImageView) itemView.findViewById(R.id.iv_mark);
            tv_market_price = (TextView) itemView.findViewById(R.id.tv_market_price);
            ll_discount = (LinearLayout) itemView.findViewById(R.id.ll_discount);
        }
    }
}
